# build.py
import os
import shutil
import PyInstaller.__main__
from pathlib import Path

def build_app():
    # 清理之前的构建
    if os.path.exists('dist'):
        shutil.rmtree('dist')
    if os.path.exists('build'):
        shutil.rmtree('build')
    
    # PyInstaller 参数
    args = [
        'src/main.py',
        # '--onefile',
        # '--windowed',
        '--name=ScriptBoy',
        '--icon=assets/icons/app.ico',
        '--add-data=src/qml;qml',
        # '--add-data=translations;translations',
        # 包含 PySide6 插件
        # '--add-data=.venv/Lib/site-packages/PySide6/plugins;PySide6/plugins',
        
        # 添加 PySide6 QML 插件
        # f'--add-data={get_pyside6_path()}/qml:PySide6/qml',
        # 隐藏导入
        # '--hidden-import=PySide6.QtCore',
        # '--hidden-import=PySide6.QtGui',
        # '--hidden-import=PySide6.QtQml',
        # '--hidden-import=PySide6.QtQuick',
        # '--hidden-import=PySide6.QtQuickControls2',
        # '--hidden-import=PySide6.QtQuickWidgets',
        # '--clean'
        
        # 排除不必要的模块
        # '--exclude-module=tkinter',
        '--clean'
    ]
    
    PyInstaller.__main__.run(args)
    
def get_pyside6_path():
    """获取 PySide6 安装路径"""
    import PySide6
    return str(Path(PySide6.__file__).parent)

if __name__ == '__main__':
    build_app()
    shutil.copytree('platform-tools', 'dist/ScriptBoy/platform-tools', dirs_exist_ok=True)